/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/5/8 21:42
 */
package org.mechanic.fund.service.tools.impl;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import org.mechanic.fund.service.tools.IQiniuService;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * 请在此写下该类的说明
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class QiniuService implements IQiniuService {
    private static final String QINUI_SERVER = "http://qiniu.g-easy.com.cn";
    private static final String ACCESS_KEY = "g9n2gXqQ5SQuu1YQw8R9NTdd9SY1iyxm2e15iPgT";
    private static final String SECRET_KEY = "XDWpagJhQUdcFpuR_Y9BDT9boKQCnHLqLKII9Anc";
    private static final String BUCKET = "mechanic";
    private static final String KEY = "1.amr";
    private static final String FILE_PATH = "/Users/mechanic/Desktop/amr/1S6TbW_VAulriT8RfBN4daHKSRtZSjSGM0-i_Uhl8rsjsHU20MzueyVT_f1fEhd_5238288396706139540.amr";

    public String amr2Mp3() {

        //String fops = "avthumb/wav";
        //String urlbase64 = UrlSafeBase64.encodeToString("1.mp3");
        //String pfops = fops + "|saveas/" + urlbase64;

        File file = new File(FILE_PATH);

        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

        String saveWavEntry = String.format("%s:xxx1.wav", BUCKET);
        StringMap putPolicy = new StringMap();
        String avthumbMp4Fop = String.format("avthumb/wav|saveas/%s", UrlSafeBase64.encodeToString(saveWavEntry));
        putPolicy.put("persistentOps", avthumbMp4Fop);
        String token = auth.uploadToken(BUCKET, "xxx1.wav", 3600, putPolicy);
        Configuration configuration = new Configuration();
        UploadManager manager = new UploadManager(configuration);
        try {
            Response response = manager.put(file, null, token);

            String result =  response.bodyString();
            System.out.println(result);
            return result;
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args){
        new QiniuService().amr2Mp3();
    }
}
